﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL_V1.App_Code.DBBroker;
using EntityObjects;


namespace BAL.Controller
{
    public class MakeStationeryAdjController
    {

        private static List<Category> categ_list;
        private static List<Catalogue> catal_list;
        private static string adjVoucher_id;

        //singleton
        private static bool instance_flag = false;
        private static MakeStationeryAdjController instance;

        public static MakeStationeryAdjController getMakeStationeryAdjController()
        {
            if (!instance_flag)
            {
                instance = new MakeStationeryAdjController();
                instance_flag = true;

                return instance;
            }
            else
            {
                return instance;
            }
        }

        public List<Category> selectAllCategory()
        {
                    categ_list = new List<Category>();
                    categ_list = CatalogueDBBroker.getCatalogueDBBroker().selectAllCategory();
                    return categ_list;
        }
        public List<Catalogue> selectAllCatalogue()
        {
                    catal_list = new List<Catalogue>();

                    catal_list = CatalogueDBBroker.getCatalogueDBBroker().selectAllCatalogue();
                    return catal_list;
        }

        public List<Catalogue> selectCatalogueByCategoryId(int Id)
        {
                    catal_list = new List<Catalogue >();
                    catal_list = CatalogueDBBroker.getCatalogueDBBroker().selectCatalogueByCategoryId(Id);
                    return catal_list;
        }

        public int updateCatalogue(Catalogue catalogue, String Id)
        {
                    int result = CatalogueDBBroker.getCatalogueDBBroker().updateCatalogue(catalogue, Id);
                    return result;
        }

        public int updateCatalogueTotalQty(Catalogue catalogue, String Id)
        {
            int result = CatalogueDBBroker.getCatalogueDBBroker().updateCatalogueTotalQty(catalogue, Id);
            return result;
        }


        public int InsertAdjVoucher(String adjDate, String selCategory,long RequserId)
        {           
                    Adjustment  adj_Obj = new Adjustment();
                    int c = AdjustmentDBBroker.getAdjustmentDBBroker().getAdjMaxSerialNo();
                    int d = c+1;
                    string maxRowID = Convert.ToString(d);
                    adjVoucher_id = selCategory.Substring(0,3) + "_"+ maxRowID;
                   
            

                    adj_Obj.AdjVoucherId=adjVoucher_id;
                    adj_Obj.AdjDate =adjDate;
                  
                    adj_Obj.AdjStatus ="Pending";
                    
                    adj_Obj.RequestedBy = RequserId;
                    int result = AdjustmentDBBroker.getAdjustmentDBBroker().addAdjustment(adj_Obj);
                    return result;
         }


        public int Process(List<AdjustmentCatalogue> Adjlist, List<Catalogue> Catlist, string SelCat, string submit_date, long RequserId)
        { 
                    int r=0;
                    Catalogue update_obj = new Catalogue();

                    //To get Adjustment Voucher
                    int r_insert = InsertAdjVoucher(submit_date, SelCat, RequserId);

              foreach (AdjustmentCatalogue obj in Adjlist)
              {
                   string selCatlid = obj.ItemId;
                   Catalogue myCatObj = Catlist.Find(delegate(Catalogue cp) { return cp.ItemId == selCatlid; });

                   if (obj.Reason == "Damage" && myCatObj.TotalQty >= obj.AdjQty) myCatObj.TotalQty = myCatObj.TotalQty - obj.AdjQty;
                   if (obj.Reason == "Get") myCatObj.TotalQty = myCatObj.TotalQty + obj.AdjQty;
                   //update TotalQty at Catalogue table
                   int result = MakeStationeryAdjController.getMakeStationeryAdjController().updateCatalogueTotalQty(myCatObj, selCatlid);

                   //update to Adjustment Detail table
                   int r_indtl = InsertDetail(obj);
              }

                  return r;

        }

        public int InsertDetail(AdjustmentCatalogue Gv_adObj)
        {
                
                AdjustmentDetails AdtlObj = new AdjustmentDetails();
                AdtlObj.AdjVoucherId = adjVoucher_id;
                AdtlObj.ItemId=Gv_adObj.ItemId;
                AdtlObj.Reason=Gv_adObj.Reason;
                AdtlObj.Quantity=Gv_adObj.AdjQty;
                int r = AdjustmentDBBroker.getAdjustmentDBBroker().addAdjustmentDetails(AdtlObj);
                return r;


        }

     

    }
}
